//
//  LXScheduleViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/16.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXScheduleViewController.h"
#import "LXScheduleTableViewCell.h"
#import "LXConverstaionViewController.h"
#import "LXScheduleModel.h"
#import "LXAppointmemtDetailVC.h"
@interface LXScheduleViewController ()<UITableViewDelegate,UITableViewDataSource>
// 1 选中今日  2 选择全部
@property(nonatomic)NSInteger isSelWho;

@property(nonatomic)UIView *todayView;
@property(nonatomic)UILabel *todayLab1;
@property(nonatomic)UILabel *todayLab2;
@property(nonatomic)UIImageView *todayImageV;


@property(nonatomic)UIView *allView;
@property(nonatomic)UILabel *allLab1;
@property(nonatomic)UILabel *allLab2;
@property(nonatomic)UIImageView *allImageV;

@property(nonatomic)UILabel *tipsLab;


@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger page;

@end

@implementation LXScheduleViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO,kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"日程提醒"];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    
    self.data = [NSMutableArray array];
    
    UIView *todayView = [[UIView alloc] initWithFrame:CGRectMake(10, kViewTopHeight + 10, (ScreenWidth - 25)/2, 84)];
    todayView.backgroundColor = [UIColor whiteColor];
    self.todayView = todayView;
    [self.view addSubview:todayView];
    [todayView addCorner:4 AndLine:mainColor];
    [todayView whenTapped:^{
        self.isSelWho = 1;
        [self selectView];
        self.page = 1;
        [self loadData];
    }];
    
    UILabel *todayLab1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 70, 20)];
    todayLab1.text = @"今日预约";
    todayLab1.textColor = mainColor;
    todayLab1.font = [UIFont systemFontOfSize:14];
    [todayView addSubview:todayLab1];
    self.todayLab1 = todayLab1;
    
    UILabel *todayLab2 = [[UILabel alloc] initWithFrame:CGRectMake(15, todayLab1.Y_height + 14, 70, 20)];
    todayLab2.text = @"587";
    todayLab2.textColor = mainColor;
    todayLab2.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    [todayView addSubview:todayLab2];
    self.todayLab2 = todayLab2;
  
    UIImage *todayImage = [UIImage imageNamed:@"今日预约日程"];
    UIImageView *todayImageV = [[UIImageView alloc] initWithFrame:CGRectMake(todayView.width - 15 - todayImage.size.width, 12, todayImage.size.width, todayImage.size.height)];
    todayImageV.image = todayImage;
    [todayView addSubview:todayImageV];
    self.todayImageV = todayImageV;
    
    
    
    UIView *allView = [[UIView alloc] initWithFrame:CGRectMake(todayView.X_width + 5, kViewTopHeight + 10, (ScreenWidth - 20)/2, 84)];
    allView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:allView];
    self.allView = allView;
    [allView addCorner:4 AndLine:mainColor];
    [allView whenTapped:^{
        self.isSelWho = 2;
        [self selectView];
        self.page = 1;
        [self loadData];
    }];
    
    UILabel *allLab1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 70, 20)];
    allLab1.text = @"全部预约";
    allLab1.textColor = mainColor;
    allLab1.font = [UIFont systemFontOfSize:14];
    [allView addSubview:allLab1];
    self.allLab1 = allLab1;
    
    UILabel *allLab2 = [[UILabel alloc] initWithFrame:CGRectMake(15, allLab1.Y_height + 14, 70, 20)];
    allLab2.text = @"587";
    allLab2.textColor = mainColor;
    allLab2.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    [allView addSubview:allLab2];
    self.allLab2 = allLab2;
  
    UIImage *allImage = [UIImage imageNamed:@"全部预约日程"];
    UIImageView *allImageV = [[UIImageView alloc] initWithFrame:CGRectMake(todayView.width - 15 - todayImage.size.width, 12, todayImage.size.width, todayImage.size.height)];
    allImageV.image = allImage;
    [allView addSubview:allImageV];
    self.allImageV = allImageV;
    
    
    UILabel *tipsLab = [[UILabel alloc] initWithFrame:CGRectMake(15, todayView.Y_height + 14, 200, 20)];
    tipsLab.text = @"今日预约日程";
    tipsLab.textColor = ColorWith333333;
    tipsLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.view addSubview:tipsLab];
    self.tipsLab = tipsLab;
    
    
    self.isSelWho = 1;
    [self selectView];
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.page++;
        [self loadData];
    }];
    
    
}
-(void)selectView{
    if(self.isSelWho == 1){
        [self.todayView addCorner:4 AndLine:mainColor];
        self.todayLab1.textColor = mainColor;
        self.todayLab2.textColor = mainColor;
        
        [self.allView addCorner:4  AndLine:UIColor.clearColor];
        self.allLab1.textColor = ColorWithAAAAAA;
        self.allLab2.textColor = ColorWith333333;
        
        self.tipsLab.text = @"今日预约日程";
    }
    
    else {
        
        [self.allView addCorner:4 AndLine:mainColor];
        self.allLab1.textColor = mainColor;
        self.allLab2.textColor = mainColor;
        
        [self.todayView addCorner:4  AndLine:UIColor.clearColor];
        self.todayLab1.textColor = ColorWithAAAAAA;
        self.todayLab2.textColor = ColorWith333333;
    
        self.tipsLab.text = @"全部预约日程";
    }
    
}

-(void)loadData{
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetSchedulet] params:@{@"today":self.isSelWho==1?@(YES):@(NO)} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if (isSuccess) {
            
            
            self.todayLab2.text = [NSString stringWithFormat:@"%@",resonseObj[@"toDayScheduleCount"]];
            self.allLab2.text = [NSString stringWithFormat:@"%@",resonseObj[@"totalScheduleCount"]];
            
            if ([self.tableView.mj_header isRefreshing]||self.page==1) {
                [self.data removeAllObjects];
                [self.tableView.mj_footer resetNoMoreData];
            }
            
            
            NSArray *list = resonseObj[@"records"];
            [self.data addObjectsFromArray:[LXScheduleModel mj_objectArrayWithKeyValuesArray:list]];
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
            [self isHaveEmptyData];
            
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LXScheduleModel *model = self.data[indexPath.section];
//
    LXScheduleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXScheduleTableViewCell class]) forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    if (self.isSelWho == 1) {
        cell.timeLab.textColor = [UIColor colorWithHexString:@"#FF921C"];
    }
    else {
        cell.timeLab.textColor = ColorWith333333;
    }
    
    [cell.kefuCallBtn whenTapped:^{
        [MSUtils callPhone:model.businessPhone];
    }];
    [cell.kefuChatBtn whenTapped:^{
        LXConverstaionViewController *conversationVC = [[LXConverstaionViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:model.businessImAccount];
        conversationVC.conversationType = ConversationType_PRIVATE;
        conversationVC.targetId = model.businessImAccount;
        conversationVC.textStr = model.businessName;

        conversationVC.enableNewComingMessageIcon = YES; //开启消息提醒
        conversationVC.enableUnreadMessageIcon = YES;
        [self.navigationController pushViewController:conversationVC animated:YES];
    }];
    
    cell.model = model;


    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LXScheduleModel *model = self.data[indexPath.section];
    LXAppointmemtDetailVC *vc = [[LXAppointmemtDetailVC alloc] init];
    vc.Id = model.entityId;
    vc.fromVc = NSStringFromClass([self class]);
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    vv.backgroundColor = self.view.backgroundColor;
    return vv;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.tipsLab.Y_height + 5, ScreenWidth, kScreenHeight - self.tipsLab.Y_height - 5) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 10;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
        _tableView.estimatedRowHeight = 113;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXScheduleTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXScheduleTableViewCell class])];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}

@end
